package com.example.admin.smstest.utils;

import com.example.admin.smstest.entity.Date;

import java.util.Calendar;

import static com.example.admin.smstest.utils.ByteUtil.hexStringToBytes;

/**
 * 日期私有协议工具类
 * @author admin
 *
 */
public class DateUtil {
	/**
	 * 获取接受数据时间
	 * @param time hexString
	 * @return
	 */
	public static Date getDataTime(String time){
		String dateTime = Integer.toBinaryString(Integer.valueOf(getTimeTurn(time),16));
		String dateTime32 = getString(dateTime,32);

		String yearString = dateTime32.substring(0, 6);
		String monthString = dateTime32.substring(6,10);
		String dayString = dateTime32.substring(10,15);
		String hourString = dateTime32.substring(15,20);
		String minuteString = dateTime32.substring(20,26);
		String secondString = dateTime32.substring(26,32);

		Date date = new Date();
		date.setYear(Integer.parseInt(yearString,2));
		date.setMonth(Integer.parseInt(monthString,2));
		date.setDay(Integer.parseInt(dayString,2));
		date.setHour(Integer.parseInt(hourString,2));
		date.setMinute(Integer.parseInt(minuteString,2));
		date.setSecond(Integer.parseInt(secondString,2));
		return date;
	}

	/**
	 * 获取发送信息 hex类型的时间
	 * @param nowLongTime = System.currentTimeMillis()
	 * @return hexString 时间
	 */
	public static String getTime(long nowLongTime){
		String timeString = getTimeLong(nowLongTime);
		return getTimeTurn(timeString);
	}

	//转换时间成16进制的String
	public static String getTimeLong(long nowLongTime){
		String timeString = getTimeString(nowLongTime);//获取时间的long类型。
		return binaryString2hexString(timeString);
	}

	//二进制String 转  十六进制String
	public static String binaryString2hexString(String bString){
		if (bString == null || bString.equals("") || bString.length() % 8 != 0)
			return null;
		StringBuffer tmp = new StringBuffer();
		int iTmp = 0;
		for (int i = 0; i < bString.length(); i += 4)
		{
			iTmp = 0;
			for (int j = 0; j < 4; j++)
			{
				iTmp += Integer.parseInt(bString.substring(i + j, i + j + 1)) << (4 - j - 1);
			}
			tmp.append(Integer.toHexString(iTmp));
		}
		return tmp.toString();
	}

	/**
	 * 获取二进制String类型时间
	 * @param nowLongTime
	 * @return
	 */
	public static String getTimeString(long nowLongTime){
		Calendar cal = Calendar.getInstance();
		int year = (cal.get(Calendar.YEAR) - 2000);
		int month = cal.get(Calendar.MONTH) + 1;
		int day = cal.get(Calendar.DAY_OF_MONTH); //本月中的第几天
		int day1 = cal.get(Calendar.DATE); //日期
		int day2 = cal.get(Calendar.DAY_OF_WEEK) + 1; //一年中的第几周
		int day3 = cal.get(Calendar.DAY_OF_WEEK_IN_MONTH); //本月中的第几周
		int day4 = cal.get(Calendar.DAY_OF_YEAR); //一年中的第几天
		int hour = cal.get(Calendar.HOUR_OF_DAY);
		int minute = cal.get(Calendar.MINUTE);
		int second = cal.get(Calendar.SECOND);

		String yearString = getString(Integer.toBinaryString(year),6);
		String monthString = getString(Integer.toBinaryString(month),4);
		String dayString = getString(Integer.toBinaryString(day),5);
		String hourString = getString(Integer.toBinaryString(hour),5);
		String minuteString = getString(Integer.toBinaryString(minute),6);
		String secondString = getString(Integer.toBinaryString(second),6);

		return yearString+monthString+dayString+hourString+minuteString+secondString;
	}

	/**
	 * 规范时间的二进制
	 * @param s 时间二进制
	 * @param length 规范
	 * @return 规范的二进制
	 */
	public static String getString(String s,int length){
		int len = length - s.length();
		if(s.length() < length){for(int i = 0 ;i < len ; i++){s = "0"+s;}}
		return s;
	}

	public static String getTimeTurn(String timeString){
		String time1 = timeString.substring(0, 2);
		String time2 = timeString.substring(2, 4);
		String time3 = timeString.substring(4, 6);
		String time4 = timeString.substring(6, 8);
		return time4 + time3 + time2 + time1;
	}
}
